package bdz.projects.lavender.opsm.ui
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.*;
	import bdz.common.events.GenericEvent;
	import com.greensock.TweenMax;
	import com.greensock.easing.*;
	import com.lav.utils.GATrackerUtil;
	/**
	 * ...
	 * @author ...
	 */
	public class MaskedImageStrip extends Sprite {
		private var _base:Sprite;
		private var _mask:Sprite;
		private var _contents:Array;
		private var _rendered:Boolean = false;
		private var _itemRef:Array;
		private var _renderer:String = "ImageStripRenderer";
		private var _padding:Number = 10;
		private var _iwidth:Number = 380;
		private var _bwidth:Number = 210;
		private var _wdiff:Number = 185;
		private var _maxItems:Number;
		private var _woffset:Number = 100;
		private var _curindex:Number = 0;
		private var _lastSelected:ImageStripItem;
		private var _larrow:String = "LeftArrowRenderer";
		private var _rarrow:String = "RightArrowRenderer";
		private var _leftArrowNav:MovieClip;
		private var _rightArrowNav:MovieClip;
		
		private var _tweening:Boolean = false;
		private var _tracker:GATrackerUtil;
		private var trackingType:String = "OPSMCarousel";
		
		import com.lav.utils.GATrackerUtil;
		function MaskedImageStrip() {
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
			
		}
		private function initializeComponent(evt:Event):void {
			_tracker = GATrackerUtil.getInstance("UA-1872680-2", this);
			_mask = new Sprite();
			addChild(_mask);
			_base = new Sprite();
			addChild(_base);
			var larr:Object = getDefinitionByName(_larrow);
			var rarr:Object = getDefinitionByName(_rarrow);
			
			_leftArrowNav = new larr() as MovieClip;
			_rightArrowNav = new rarr() as MovieClip;
			
			_leftArrowNav.addEventListener(MouseEvent.CLICK, scrollLeft);
			_rightArrowNav.addEventListener(MouseEvent.CLICK, scrollRight);
			_leftArrowNav.buttonMode = _leftArrowNav.useHandCursor = true;
			_rightArrowNav.buttonMode = _rightArrowNav.useHandCursor = true;
			_leftArrowNav.y -= 20;
			_rightArrowNav.y -= 20;
			_leftArrowNav.visible = _rightArrowNav.visible = false;
			addChild(_leftArrowNav);
			addChild(_rightArrowNav);
			
			
			if (_contents) {
				startRendering();
			}else {
				drawMask();
			}
			stage.addEventListener(Event.RESIZE, onStageResize);
			
		}
		private function onStageResize(evt:Event = null):void {
			try{
				_maxItems = Math.floor((Math.ceil(stage.stageWidth - (_wdiff + _woffset)) / (_bwidth)));
				if (_maxItems > _contents.length) {
					_maxItems = _contents.length;
				}
			}catch(e:Error){}
			try{
				drawMask();
				
			}catch(e:Error){}
		}
		private function checkItems():void {
			_leftArrowNav.visible = _rightArrowNav.visible = (_maxItems < _contents.length);
			if (_itemRef && _rendered && _lastSelected != _itemRef[_curindex]) {
				_itemRef[_curindex].forceExpand();
			}
		}
		private function drawMask():void {
			try{
				var diff:Number = Math.ceil(_mask.x - _base.x);
				
				_mask.graphics.clear();
				_mask.graphics.beginFill(0x60A0C9, 1);
				_mask.graphics.drawRect(0, 0, Math.ceil(Math.ceil(_maxItems * (_bwidth+_padding)) + Math.ceil(_wdiff-_padding*2)), stage.stageHeight);
				_mask.graphics.endFill();
				
				if (diff > 10) {
					TweenMax.to(_base, .6, { x:(((stage.stageWidth - _mask.width) / 2)-diff), ease:Strong.easeOut } );
				}else {
					TweenMax.to(_base, .6, { x:((stage.stageWidth - _mask.width) / 2), ease:Strong.easeOut } );
				}
				
				TweenMax.to(_mask, .6, { x:(Math.ceil(stage.stageWidth - _mask.width) / 2), ease:Strong.easeOut } );
				_leftArrowNav.y = _rightArrowNav.y = Math.ceil(stage.stageHeight / 2)-10;   
				_leftArrowNav.x = (Math.ceil(stage.stageWidth - _mask.width) / 2) / 2;
				_rightArrowNav.x = (Math.ceil(stage.stageWidth - _mask.width) / 2) + _mask.width + _leftArrowNav.x;
				_base.y = 8;
			}catch(e:Error){}
		}
		private function scrollLeft(evt:MouseEvent = null):void {
			if (_tweening) return;
			if (_curindex > 0) {
				_curindex--;
				TweenMax.to(_base, .9, { x:(_base.x + (_bwidth + _padding)), ease:Strong.easeInOut, onStart:function(){_tweening = true}, onComplete:function() {_tweening =false} } );
				_itemRef[_curindex].forceExpand();
			}
			
		}
		private function scrollRight(evt:MouseEvent = null):void {
			if (_tweening) return;
			if (_curindex < (_contents.length - _maxItems)) {
				_curindex++;
				TweenMax.to(_base, .9, { x:(_base.x - (_bwidth + _padding)), ease:Strong.easeInOut, onStart:function(){_tweening = true}, onComplete:function() {_tweening =false} } );
				_itemRef[_curindex].forceExpand();
			}
		}
		private function onItemChange(evt:GenericEvent):void {
			for (var i:Number = 0; i < _contents.length; i++) {
				if(i != evt.data.index && i != 0){
					_itemRef[i].x = (_itemRef[i - 1].x + _itemRef[i - 1].width) + _padding;
				}
			}
		}
		private function onItemRollOver(evt:GenericEvent):void {
			if(_lastSelected != evt.target){
				evt.target.expand();
				if (_lastSelected) {
					_lastSelected.contract();
				}
				_lastSelected = evt.target as ImageStripItem;
			}
		}
		
		private function startRendering():void {
			if (_itemRef && _itemRef.length) {
				for each(var mc:ImageStripItem in _itemRef) {
					try{
						_base.removeChild(mc);
					}catch(e:Error) {}
				}
			}
			_itemRef = [];
			onStageResize();
			_base.mask = _mask;
			var sx:Number = 0;
			for (var i:int = 0; i < _contents.length; i++) {
				var clz:Object = getDefinitionByName(_renderer);
				var citem:ImageStripItem = new clz() as ImageStripItem;
				citem.addEventListener(GenericEvent.ROLL_OVER, onItemRollOver);
				citem.addEventListener(GenericEvent.STARTED, onItemChange);
				citem.addEventListener(GenericEvent.CLOSE, onItemChange);
				citem.addEventListener(GenericEvent.CLICKED, onItemClicked);
				citem.expandedWidth = _iwidth;
				citem.index = i;
				citem.expanded = i == 0;
				citem.data = _contents[i];
				citem.x = Math.ceil(sx);
				
				sx += Math.ceil(_bwidth + _padding);
				_itemRef.push(_base.addChild(citem));
			}
			_rendered = true;
			setTimeout(checkItems, 1000);
		}
		private function onItemClicked(evt:GenericEvent):void {
			if (evt.data.url.indexOf(".flv")) {
				_tracker.trackEvent(trackingType+"/VideoItemClick", evt.data.url);
			}else {
				_tracker.trackEvent(trackingType+"/URLItemClick", evt.data.url);
			}
		}
		public function set contents(arr:Array):void {
			_contents = arr;
			if (stage) {
				startRendering();
			}
		}
		public function get contents():Array {
			return _contents;
		}
		
	}
	
}
